<template>
  <div>
    <h1>参与活动</h1>
  </div>
  <el-form :model="searchForm" :inline="true">
    <!-- <el-form-item label="关键字">
      <el-select
        v-model="searchForm.typeId"
        placeholder="please select your zone"
      >
        <el-option label="Zone one" value="" />
        <el-option label="Zone two" value="beijing" />
      </el-select>
    </el-form-item> -->

    <el-form-item label="拼团活动名称">
      <el-input v-model="searchForm.keyWord"></el-input>
    </el-form-item>
    <el-form-item label="开始时间">
      <div class="block">
        <el-date-picker
          v-model="searchForm.startTime"
          type="datetime"
          placeholder="Select date and time"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </div>
    </el-form-item>
    <el-form-item label="结束时间">
      <div class="block">
        <el-date-picker
          v-model="searchForm.endTime"
          type="datetime"
          placeholder="Select date and time"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </div>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="LoadList()">搜索</el-button>
    </el-form-item>
  </el-form>
  <el-button type="primary" @click="saveGroupAct()">添加拼团活动</el-button>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="activeId" label="活动id" width="180" />
      <el-table-column prop="joinName" label="参与拼团名称" />
      <el-table-column prop="joinTip" label="拼团活动简介" />
      <el-table-column prop="prodUnit" label="商品单位">
        <template #default="scope">
          <span v-if="scope.row.userFlag == 1">件</span>
          <span v-if="scope.row.userFlag == 2">套</span>
          <span v-if="scope.row.userFlag == 3">对</span>
          <span v-if="scope.row.userFlag == 4">床</span>
          <span v-if="scope.row.userFlag == 5">条</span>
        </template>
      </el-table-column>
      <el-table-column prop="joinImages" label="商品轮播">
        <template #default="scope">
          <el-image
            :src="scope.row.joinImages"
            style="width: 50; height: 50"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="beginTime" label="开始时间" />
      <el-table-column prop="endTime" label="结束时间" />
      <el-table-column prop="expireTime" label="拼团失效" />
      <el-table-column prop="groupNum" label="拼团人数" />
      <el-table-column prop="buyMax" label="总购买限制" />
      <el-table-column prop="virtualNum" label="补齐人数" />
      <el-table-column prop="prodPrice" label="搜原价" />
      <el-table-column prop="groupPrice" label="拼团价格" />
      <el-table-column prop="prodInfoId" label="拼团id" />
      <el-table-column prop="sendKind" label="配送方式" />
      <el-table-column prop="sendPay" label="运费设置" />
      <el-table-column prop="sendMoney" label="运费" />
      <el-table-column prop="joinTop" label="排序" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success">编辑</el-button>
          <el-button type="danger" @click="deleteAct(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-dialog
        v-model="dialogFormVisible"
        title="Shipping address"
        width="800"
      >
        <el-form :model="saveForm">
          <el-form-item label="拼团活动名称" :label-width="formLabelWidth">
            <el-input v-model="saveForm.joinName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="总购买数量限制" :label-width="formLabelWidth">
            <el-input v-model="saveForm.buyMax" autocomplete="off" />
          </el-form-item>
          <el-form-item label="拼团开始时间" :label-width="formLabelWidth">
            <div class="block">
              <el-date-picker
                v-model="saveForm.beginTime"
                type="datetime"
                placeholder="Select date and time"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </div>
          </el-form-item>
          <el-form-item label="拼团结束时间" :label-width="formLabelWidth">
            <div class="block">
              <el-date-picker
                v-model="saveForm.endTime"
                type="datetime"
                placeholder="Select date and time"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </div>
          </el-form-item>
          <el-form-item label="拼团时效（小时）" :label-width="formLabelWidth">
            <el-input v-model="saveForm.expireTime" autocomplete="off" />
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input v-model="saveForm.joinTip" autocomplete="off" />
          </el-form-item>
          <el-form-item label="拼团人数" :label-width="formLabelWidth">
            <el-input v-model="saveForm.groupNum" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品原价" :label-width="formLabelWidth">
            <el-input v-model="saveForm.prodPrice" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品轮播图" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/team/index/uploadImage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品单位" :label-width="formLabelWidth">
            <el-select v-model="saveForm.prodUnit" placeholder="请选择">
              <el-option
                v-for="item in UnitOptions"
                :label="item.dictName"
                :value="item.dictValue"
                :key="dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="配送方式" :label-width="formLabelWidth">
            <el-input v-model="saveForm.sendKind" autocomplete="off" />
          </el-form-item>
          <el-form-item label="运费" :label-width="formLabelWidth">
            <el-input v-model="saveForm.sendMoney" autocomplete="off" />
          </el-form-item>
          <el-form-item label="运费设置" :label-width="formLabelWidth">
            <el-input v-model="saveForm.sendPay" autocomplete="off" />
          </el-form-item>
          <el-form-item label="补齐人数" :label-width="formLabelWidth">
            <el-input v-model="saveForm.virtualNum" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">Cancel</el-button>
            <el-button type="primary" @click="saveActive()"> 添加 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="searchForm.pageNum"
      v-model:page-size="searchForm.pageSize"
      :page-sizes="[100, 200, 300, 400]"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
const tableData = ref([]);
import { groupJoin, unitList, addActiveJoin, deleteJoinAct } from "@/api/api";
import { ElMessage } from "element-plus";
const searchForm = ref({
  keyWord: "",
  pageNum: 1,
  pageSize: 2,
  typeId: null,
  endTime: "",
  startTime: "",
});
const total = ref(0);
const background = ref(false);
const disabled = ref(false);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  LoadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  LoadList();
};

const LoadList = () => {
  groupJoin(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("展示成功");
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  LoadList();
  dictlist();
});
//添加拼团活动
const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const saveForm = ref({
  beginTime: "",
  buyMax: null,
  endTime: "",
  expireTime: null,
  groupNum: null,
  groupPrice: null,
  joinId: null,
  joinImages: "",
  joinName: "",
  joinTip: "",
  joinTop: null,
  prodInfoId: null,
  prodPrice: null,
  prodUnit: null,
  sendKind: null,
  sendMoney: null,
  sendPay: null,
  virtualNum: null,
});

const saveGroupAct = () => {
  saveForm.value = {
    beginTime: "",
    buyMax: null,
    endTime: "",
    expireTime: null,
    groupNum: null,
    groupPrice: null,
    joinId: null,
    joinImages: "",
    joinName: "",
    joinTip: "",
    joinTop: null,
    prodInfoId: null,
    prodPrice: null,
    prodUnit: null,
    sendKind: null,
    sendMoney: null,
    sendPay: null,
    virtualNum: null,
  };
  dialogFormVisible.value = true;
};
const UnitOptions = ref([]);
const dictlist = () => {
  unitList().then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("展示成功");
      UnitOptions.value = res.data.data;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
//图片
import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";
import { tr } from "element-plus/es/locale";

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  // imageUrl.value = URL.createObjectURL(uploadFile.raw!);
  imageUrl.value = response.data;
};
const saveActive = () => {
  addActiveJoin(saveForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      LoadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const deleteAct = (row: any) => {
  let joinId = row.joinId;
  deleteJoinAct(joinId).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("删除成功");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
